package com.google.code.whitetask.old;

import static com.google.code.whitetask.old.TaskResultType.*;

import java.util.ArrayList;
import java.util.List;

public class TaskChain extends AbstractFluentTask {
  private List<Task> tasks = new ArrayList<Task>();
  private TaskProxy proxy;

  public TaskChain add(Task task) {
    tasks.add(task);
    return this;
  }

  public TaskResultType execute(TaskContext context) {
    for (Task each : tasks) {
      TaskResultType result = null;
      if (proxy == null) {
        result = each.execute(context);
      } else {
        result = proxy.execute(each, context);
      }
      validateTaskResult(each, result);
    }
    return TaskResultType.SUCCESS;
  }

  public TaskChain proxy(TaskProxy taskProxy) {
    this.proxy = taskProxy;
    return this;
  }

  public TaskProxy getProxy() {
    return this.proxy;
  }
  
  int getNumberOfTasks() {
    return tasks.size();
  }
}
